package com.augmentum.oes.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.augmentum.oes.Constants;
import com.augmentum.oes.dao.QuestionDao;
import com.augmentum.oes.exception.ParameterException;
import com.augmentum.oes.exception.ServiceException;
import com.augmentum.oes.model.AnswerRadioEnum;
import com.augmentum.oes.model.Pagination;
import com.augmentum.oes.model.Question;
import com.augmentum.oes.model.SortStatusEnum;
import com.augmentum.oes.service.QuestionService;
import com.augmentum.oes.util.Regex;
import com.augmentum.oes.util.StringUtil;

@Service
public class QuestionServiceImpl implements QuestionService {

    private final static String ENTER_QUESTION_MSG = "Please enter a question!";
    private final static String ENTER_ANSWER_MSG = "Please enter a answer!";
    private final static String ENTER_RIGHT_ANSWER_MSG = "Please select the right option!";
    private static final String NO_DATA = "There is no query to the data!";
    private static final String QUESTION_ID_REQUIRED = "Question id is required!";
    private static final String QUESTION_REQUIRED = "Question is required!";
    private static final int PAGE_SIZE = 20;

    private static final String QUESTION_A = "A";
    private static final String QUESTION_B = "B";
    private static final String QUESTION_C = "C";
    private static final String QUESTION_D = "D";
    private static final String DEFAULT_SORT = "ASC";

    @Resource
    private QuestionDao questionDao;

    public void setQuestionDao(QuestionDao questionDao) {
        this.questionDao = questionDao;
    }

    public QuestionServiceImpl() {
        super();
    }

    @Override
    public Question createQuestion(Question question, String radio) throws ParameterException, ServiceException {
        ParameterException parameterException = new ParameterException();
        if (StringUtil.isEmpty(question.getQuestion())) {
            parameterException.addErrorMessage(Constants.ERROR_MESSAGE, ENTER_QUESTION_MSG);
            throw parameterException;
        }
        if (StringUtil.isEmpty(question.getAnswerA()) || StringUtil.isEmpty(question.getAnswerB())
                || StringUtil.isEmpty(question.getAnswerC()) || StringUtil.isEmpty(question.getAnswerD())) {
            parameterException.addErrorMessage(Constants.ERROR_MESSAGE, ENTER_ANSWER_MSG);
            throw parameterException;
        }
        if (StringUtil.isEmpty(radio)) {
            parameterException.addErrorMessage(Constants.ERROR_MESSAGE, ENTER_RIGHT_ANSWER_MSG);
            throw parameterException;
        }
        switch (radio) {
        case QUESTION_A:
            question.setAnswer(AnswerRadioEnum.A.getValue());
            break;
        case QUESTION_B:
            question.setAnswer(AnswerRadioEnum.B.getValue());
            break;
        case QUESTION_C:
            question.setAnswer(AnswerRadioEnum.C.getValue());
            break;
        case QUESTION_D:
            question.setAnswer(AnswerRadioEnum.D.getValue());
            break;
        default:// empty
            break;
        }
        return questionDao.save(question);
    }

    @Override
    public List<Question> getAllViewList(Pagination pagination, String sort, String keyWord) throws ServiceException {
        SortStatusEnum sortStatus = null;
        try {
            sortStatus = SortStatusEnum.valueOf(sort);
        } catch (Exception e) {
            sortStatus = SortStatusEnum.valueOf(DEFAULT_SORT);
        }
        if (!StringUtil.isEmpty(keyWord)) {
            keyWord = Regex.regex(keyWord);
        }
        if (pagination.getPageSize() > PAGE_SIZE) {
            pagination.setPageSize(PAGE_SIZE);
        }
        List<Question> list = questionDao.getAllQuestion(pagination, sortStatus, keyWord);

        if (list == null) {
            throw new ServiceException(NO_DATA);
        }
        return list;
    }

    @Override
    public Question getByQuestionId(String id) throws ParameterException {
        ParameterException parameterException = new ParameterException();
        if (StringUtil.isEmpty(id)) {
            parameterException.addErrorMessage(Constants.ERROR_MESSAGE, QUESTION_ID_REQUIRED);
        }
        if (parameterException.isErrorMessage()) {
            throw parameterException;
        }
        return questionDao.getByQuestionId(Integer.parseInt(id));
    }

    @Override
    public void updateQuestion(Question question, String radio, String[] ansStr) throws ParameterException {
        ParameterException parameterException = new ParameterException();
        if (question == null) {
            parameterException.addErrorMessage(Constants.ERROR_MESSAGE, QUESTION_REQUIRED);
        }
        if (StringUtil.isEmpty(radio)) {
            parameterException.addErrorMessage(Constants.ERROR_MESSAGE, ENTER_RIGHT_ANSWER_MSG);
        }
        if (parameterException.isErrorMessage()) {
            throw parameterException;
        }
        switch (radio) {
        case QUESTION_A:
            question.setAnswer(AnswerRadioEnum.A.getValue());
            break;
        case QUESTION_B:
            question.setAnswer(AnswerRadioEnum.B.getValue());
            break;
        case QUESTION_C:
            question.setAnswer(AnswerRadioEnum.C.getValue());
            break;
        case QUESTION_D:
            question.setAnswer(AnswerRadioEnum.D.getValue());
            break;
        default:// empty
            break;
        }
        question.setAnswerA(ansStr[0]);
        question.setAnswerB(ansStr[1]);
        question.setAnswerC(ansStr[2]);
        question.setAnswerD(ansStr[3]);
        questionDao.update(question);
    }

    @Override
    public void dropById(String[] idStr) throws ParameterException {
        ParameterException parameterException = new ParameterException();
        if (idStr.length < 1) {
            parameterException.addErrorMessage(Constants.ERROR_MESSAGE, QUESTION_ID_REQUIRED);
        }
        if (parameterException.isErrorMessage()) {
            throw parameterException;
        }
        questionDao.dropById(idStr);
    }

    @Override
    public Question getLastQuestion() {
        return questionDao.getLastQuestion();
    }

    @Override
    public List<Question> getRandomQuestion(int row) {
        return questionDao.getRandomQuestion(row);
    }
}
